The present invention relates to multithreaded computer programs, and more particularly to the modification of such computer programs during execution.
Many modern computer programs are “multi-threaded”, that is, the computer program is split into a plurality of simultaneously executing, or virtually simultaneously executing, tasks or “threads”. In the case of a computer system having a plurality of processors, the various threads may actually execute simultaneously. In the case of a computer system having a single processor, the threads will typically be executed sequentially by the processor, with the switching between the threads being so fast as to be virtually simultaneous. While such threads typically execute independently of one another, they usually share resources such as memory.
In many instances, it is necessary or desirable for certain portions of the computer program code to be modified while the code is being executed by the computer system. In a multithreaded context, such modification presents a number of difficulties.
One problem associated with such modification is that because the threads share resources, where more than one thread attempts to modify a section of code at the same (or substantially the same) time, problems can be introduced into the code. For example, a second thread may begin modifying the code section before a first thread has completed its modifications, so that the resulting code no longer produces the desired results. When such undesired interactions between threads occur, the situation is said not to be “thread safe”; conversely, when such interactions are prevented, the code is “thread safe”.
Conventionally, there have been two main approaches used to avoid the problems associated with multiple threads attempting to modify the same section of code at the same time.
The first approach is to use a “lock” to synchronize access to the section of code so that all threads other than the thread that is modifying the section of code are prevented from executing until the modification is complete. This approach, understandably, will usually degrade the performance of the computer program.
The second approach is to direct all threads, other than the thread that is modifying the section of code, to a “spin loop” until the modification is complete. A “spin loop” is a section of code which causes the threads to carry out pointless programming tasks, that do not advance any of the objectives of the computer program, until the modifications are complete. The threads in the spin loop are in essence “spinning their wheels”, also known as “busy waiting”, while they wait for the relevant thread to finish modifying the code. The use of spin loops can lead to “live lock” situations, where a higher priority thread is trapped in the spin loop and is unable to escape because a lower priority thread is modifying the code. As a result, the program can seize or “hang” such that it is unable to proceed with further productive execution.
Accordingly, it is desirable to provide a technique by which sections of computer code can be modified during execution in a multithreaded environment, without resorting to locks or spin loops.